<template>
	<view class="container">
		<steps 
			typeCode="LiPinDuiHuan"
			apiname="getPermitSubjectGift"
			:apiparam="apiparam"
			:brief="brief" 
			:notEmpty="true"
			:disabled="disabled"
			:insflowid="insflowid"
			@confirm="confirm">
			<view class="param">
				<text class="param-title">礼品：</text>
				<text class="param-text">{{data.GiftName}}</text>
			</view>
			<view class="param">
				<text class="param-title">规格：</text>
				<text class="param-text">{{data.BatchScale}}</text>
			</view>
			<view class="param">
				<text class="param-title">描述：</text>
				<text class="param-text">{{data.BatchDesc}}</text>
			</view>
			<view class="param">
				<text class="param-title">所需积分：</text>
				<text class="param-text" style="color: #fa8c16;">{{data.CostScore*num}}</text>
			</view>
			<view class="num">
				<view class="num__left">数量</view>
				<view class="num__right">
					<u-number-box v-model="num" :min="1" :max="data.limitNum" integer @overlimit="overlimit"></u-number-box>
				</view>
			</view>
		</steps>
	</view>
</template>

<script>
	import _custom from '@/common/_custom.js'
	import _data from '@/common/_data.js'
	import { formatImg } from '@/common/custom_util.js';
	import Steps from '@/pages/home/child/add-attend-apply/components/steps.vue'
	
	export default {
		components: {
			Steps,
		},
		data() {
			return {
				obsusername: _data.localData('name'),
				pxid: _data.localData('userid'),
				id: '',//商品id
				data: {},//商品信息
				disabled: false,
				insflowid: '',
				num: 1,
				score: 0,
				max: 1,
			}
		},
		onLoad(options) {
			if(options.data) {
				this.data = JSON.parse(options.data)
				this.insflowid = options.insflowid;
			}
			this.id = options.id;
			if(!this.id) {
				uni.showToast({
					title: '请在兑换页面提交申请',
					icon: 'none'
				})
				setTimeout(() => {
					uni.navigateBack()
				},2000)
				return
			}
			this.getSubjectGiftDetail();
		},
		computed: {
			brief() {
				return '名称：' + this.data.GiftName + '|规格：' + this.data.BatchScale
			},
			apiparam() {
				return {
					obsusername: this.obsusername,
					obspxid: this.pxid,
					giftbatchid: this.id,
					applynum: this.num,
				}
			}
		},
		methods: {
			/**
			 * 获取礼品详情
			 */
			getSubjectGiftDetail() {
				let that = this;
				return new Promise((resolve,reject) => {
					_custom.getDataJava({
						apiname: 'getSubjectGiftDetail',
						apiparam: {
							obsusername: this.obsusername,
							obspxid: this.pxid,
							giftbatchid: this.id
						},
						success(res) {
							if(!res.length) return
							let data = res[0];
							data.stock = data.BatchNum*1 - data.ApplyNum*1;
							data.limitNum = data.UserLimit*1 - data.MyApplyNum*1;
							data.image = formatImg(data.BatchImage)
							that.data = data;
							resolve()
						},
						fail() {
							uni.showToast({
								title: '获取数据失败',
								icon: 'none'
							})
							reject()
						}
					})
				})
			},
			/**
			 * 添加礼品兑换申请
			 * @param {Object} apiparam  接口参数
			 */
			subjectGiftApply(apiparam) {
				return new Promise((resolve,reject) => {
					_custom.getDataJava({
						apiname: 'subjectGiftApply',
						code: 3,
						apiparam,
						success(res) {
							resolve()
						}
					})
				})
			},
			/**
			 * 触发流程环节通知
			 */
			trigFlowStepNotice(apiparam) {
				let that = this;
				return new Promise((resolve,reject) => {
					_custom.getDataJava({
						apiname: 'trigFlowStepNotice',
						code: 2,
						apiparam,
						success() {
							resolve()
						}
					})
				})
			},
			/**
			 * 点击提交按钮申请流程后的回调
			 * @param {Object} v 回调参数
			 */
			confirm(v) {
				this.subjectGiftApply({
					obsusername: this.obsusername,
					obspxid: this.pxid,
					insflowid: v.insflowid,
					giftbatchid: this.id,
					applynum: this.num,
				}).then(() => {
					uni.showToast({
						title: '流程创建成功',
						icon: 'none'
					})
					this.trigFlowStepNotice({
						insflowid: v.insflowid,
					})
					setTimeout(() => {
						uni.redirectTo({
							url: '/pages/home/child/subject-gift/index?data=' + JSON.stringify(v)
						})
					},500)
				})
			},
			overlimit(e) {
				let title = '';
				switch(e) {
					case 'plus':
						title = '已达最大值';
						break;
					case 'minus':
						title = '已达最小值';
						break;
				}
				uni.showToast({
					title,
					icon: 'none'
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.param {
		display: flex;
		align-items: center;
		height: 100rpx;
		background-color: #fff;
		padding: 0 20rpx;
		box-sizing: border-box;
		
		&-title {
			width: 150rpx;
			text-align: center;
		}
		
		&-text {
			flex: 1;
		}
	}
	
	.num {
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 100rpx;
		background-color: #fff;
		padding: 0 20rpx;
		box-sizing: border-box;
	}
</style>
